package com.zjdiepu.www.common.util;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
 * 类描述:字符串与输入流装换 <br/>     
 * 项目名称:diepu-parent-common <br/> 
 * 类名称:StringStreamUtil <br/>     
 * 创建人:xiongxiaotun <br/> 
 * 创建时间:2017年8月16日 下午8:18:09  <br/>   
 * 修改人:xiongxiaotun <br/> 
 * 修改时间: 2017年8月16日 下午8:18:09   <br/>  
 * 修改备注:   <br/> 
 * @version V1.0   <br/>
 */
public class StringStreamUtil {
	private static final Logger logger = LoggerFactory.getLogger(StringStreamUtil.class);
	/**
	 *
	 将一个字符串转化为输入流
	 */
	public static InputStream getStringStream(String sInputString) {
		if (StringUtils.isNotBlank(sInputString)) {
			try {
				ByteArrayInputStream tInputStringStream = new ByteArrayInputStream(
						sInputString.getBytes("UTF-8"));
				return tInputStringStream;
			} catch (Exception e) {
				logger.error("字符串转成输入流.异常栈:{}", e);
			}
		}
		return null;
	}

	/**
	 *将一个输入流转化为字符串
	 */
	public static String getStreamString(InputStream tInputStream) {
		if (tInputStream != null) {
			try {
				BufferedReader tBufferedReader = new BufferedReader(
						new InputStreamReader(tInputStream,"UTF-8"));
				StringBuffer tStringBuffer = new StringBuffer();
				String sTempOneLine = new String("");
				while ((sTempOneLine = tBufferedReader.readLine()) != null) {
					tStringBuffer.append(sTempOneLine);
				}
				return tStringBuffer.toString();
			} catch (Exception e) {
				logger.error("读取输入流.异常栈:{}", e);
			}
		}
		return null;
	}
}
